<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.io.IOException" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.annotation.WebServlet" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head><title>查询所有</title></head>
<body>
<%
  Connection conn = null;
  PreparedStatement stmt = null;

  try {
	  
	  String driver = "com.mysql.cj.jdbc.Driver";
	  String dburl = "jdbc:mysql://localhost:3306/postgres";
	  String username = "root";
	  String password = "123456";
	  String sql="SELECT * FROM books";
	  try{
		  //加载数据库驱动程序
		 Class.forName(driver); 
		  //创建数据库连接对象
		  conn = DriverManager.getConnection(
		  dburl,username,password);
		 }catch(ClassNotFoundException cfe){
		  log("Exception:"+cfe);
		 }catch(SQLException sqle){
		  log("Exception:"+sqle);
		 }
    stmt=conn.prepareStatement(sql);
    ResultSet rst = stmt.executeQuery(sql);
%>

<table border="1">
  <tr>
    <th>书号</th>
    <th>书名</th>
    <th>作者</th>
    <th>出版社</th>
    <th>价格</th>
  </tr>
  <%
    while(rst.next()) {
      String bookid = rst.getString("bookid");
      String title = rst.getString("title");
      String author = rst.getString("author");
      Double price = rst.getDouble("price");
      String publisher = rst.getString("publisher");
  %>
  <tr>
    <td><%=bookid%></td>
    <td><%=title%></td>
    <td><%=author%></td>
    <td><%=price%></td>
    <td><%=publisher%></td>
  </tr>
 <%
      }
      rst.close();
      stmt.close();
      conn.close();
    } catch (Exception e) {
      out.print("查询失败！");
    }
  %>
</table>
</body>
</html>
